#pragma once
#include "oj_model2.hpp"
#include <ctemplate/template.h>

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";
    class view
    {
    public:
        view()
        {}
        ~view()
        {}
    public:

        void AllExpandHtml(std::vector<ns_model::Question>& questions, std::string* html)
        {
            //获取路径
            ctemplate::TemplateDictionary root("all_questions");
            std::string path = template_path + "all_questions.html";
            //形成数据字典
            for(const auto& s : questions)
            {
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", s.number);
                sub->SetValue("title", s.title);
                sub->SetValue("star", s.star);
            }
            //获取被渲染对象
            ctemplate::Template* tp = ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);
            //添加字典数据到网页中
            tp->Expand(html, &root);
        }

        void OneExpandHtml(ns_model::Question& question, std::string* html)
        {
            //获取路径
            ctemplate::TemplateDictionary root("question");
            std::string path = template_path + "one_question.html";
            //形成数据字典
            root.SetValue("number", question.number);
            root.SetValue("title", question.title);
            root.SetValue("star", question.star);
            root.SetValue("desc", question.desc);
            root.SetValue("pre_code", question.header);
            //获取被渲染对象
            ctemplate::Template* tp = ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);
            //添加字典数据到网页中
            tp->Expand(html, &root);
        }
    };
}